package com.zyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyf.client.DictFeignClient;
import com.zyf.enums.DictEnum;
import com.zyf.mapper.PatientMapper;
import com.zyf.model.user.Patient;
import com.zyf.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/** 就诊人管理的Service实现类
 * @author zyf
 * @date 2021年05月28日 11:30
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 根据用户id获取就诊人列表
     * @author zyf
     * @date 2021/5/28 11:38
     * @param userId 用户id
     * @return java.util.List<com.zyf.model.user.Patient>
     */
    @Override
    public List<Patient> findAllPatientByUserId(Long userId) {

        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        //通过远程调用，得到编码对应的具体内容，查询字典表里的内容
        patientList.forEach(this::packPatient);
        return patientList;
    }

    /**
     * 根据id获取就诊人信息
     * @author zyf
     * @date 2021/5/28 11:59
     * @param id 就诊人id
     * @return com.zyf.model.user.Patient
     */
    @Override
    public Patient findPatientById(long id) {
        Patient patient = baseMapper.selectById(id);
        Patient packPatient = this.packPatient(patient);
        return packPatient;
    }

    /**
     * patient对象里面，其他参数的封装
     * @author zyf
     * @date 2021/5/28 11:45
     * @param patient 就诊人对象
     */
    private Patient packPatient(Patient patient) {

        //获取证件名称
        String certificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());

        //联系人证件类型
        String contactsCertificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());

        return patient;
    }
}
